package com.yzw.oss.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.yzw.common.dto.R;
import com.yzw.oss.service.VodService;
import com.yzw.oss.utils.InitVodCilent;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @Author Yzw
 * @Description TODO
 * @Date 2023/5/13 14:34
 * @Version 1.0
 */
@RestController
@RequestMapping("/vod")
public class VodController {
    @Resource
    VodService vodService;
    //账号AK信息请填写（必选）
    @Value("${aliyun.oss.file.accessKeyId}")
    private String accessKeyId;
    //账号AK信息请填写（必选）
    @Value("${aliyun.oss.file.accessKeySecret}")
    private String accessKeySecret;

    @ApiOperation("视频上传")
    @PostMapping()
    public R getVod(MultipartFile file) {
        String vodId = vodService.getVod(file);
        return R.success("视频上传成功", vodId);
    }
    @ApiOperation("获取视频凭证接口")
    @GetMapping("{vodId}")
    public R getPath(@PathVariable String vodId){
        String auth = vodService.getPath(vodId);
        return R.success("获取凭证成功",auth);
    }

    @ApiOperation("删除视频")
    @DeleteMapping("/removeAlyVideo/{videoId}")
    public R removeAlyVideo(@PathVariable String videoId){
        try{
            // 初始化对象
            DefaultAcsClient client = InitVodCilent.initVodClient(accessKeyId, accessKeySecret);
            // 创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            // 向request设置视频id
            request.setVideoIds(videoId);
            // 调用初始化对象的方法实现删除
            client.getAcsResponse(request);
            return R.success("删除视频成功",null);
        }catch (Exception e) {
            e.printStackTrace();
            return R.error(20001, "删除视频失败");
        }
    }
}
